package com.cindata;

import java.util.HashMap;
import java.util.Map;

public class WGSTOGCJ02
{

	private static double pi = 3.1415926535897931D;
	private static double a = 6378245D;
	private static double ee = 0.0066934216229659433D;

	public WGSTOGCJ02()
	{
	}

	public boolean outofChina(double lat, double lon)
	{
		if (lon < 72.004000000000005D || lon > 137.8347D)
			return true;
		return lat < 0.82930000000000004D || lat > 55.827100000000002D;
	}

	public double transformLat(double x, double y)
	{
		double ret = -100D + 2D * x + 3D * y + 0.20000000000000001D * y * y + 0.10000000000000001D * x * y + 0.20000000000000001D * Math.sqrt(Math.abs(x));
		ret += ((20D * Math.sin(6D * x * pi) + 20D * Math.sin(2D * x * pi)) * 2D) / 3D;
		ret += ((20D * Math.sin(y * pi) + 40D * Math.sin((y / 3D) * pi)) * 2D) / 3D;
		ret += ((160D * Math.sin((y / 12D) * pi) + 320D * Math.sin((y * pi) / 30D)) * 2D) / 3D;
		return ret;
	}

	public double transformLon(double x, double y)
	{
		double ret = 300D + x + 2D * y + 0.10000000000000001D * x * x + 0.10000000000000001D * x * y + 0.10000000000000001D * Math.sqrt(Math.abs(x));
		ret += ((20D * Math.sin(6D * x * pi) + 20D * Math.sin(2D * x * pi)) * 2D) / 3D;
		ret += ((20D * Math.sin(x * pi) + 40D * Math.sin((x / 3D) * pi)) * 2D) / 3D;
		ret += ((150D * Math.sin((x / 12D) * pi) + 300D * Math.sin((x / 30D) * pi)) * 2D) / 3D;
		return ret;
	}

	public Map transform(double lon, double lat)
	{
		HashMap localHashMap = new HashMap();
		if (outofChina(lat, lon))
		{
			localHashMap.put("lon", Double.valueOf(lon));
			localHashMap.put("lat", Double.valueOf(lat));
			return localHashMap;
		} else
		{
			double dLat = transformLat(lon - 105D, lat - 35D);
			double dLon = transformLon(lon - 105D, lat - 35D);
			double radLat = (lat / 180D) * pi;
			double magic = Math.sin(radLat);
			magic = 1.0D - ee * magic * magic;
			double sqrtMagic = Math.sqrt(magic);
			dLat = (dLat * 180D) / (((a * (1.0D - ee)) / (magic * sqrtMagic)) * pi);
			dLon = (dLon * 180D) / ((a / sqrtMagic) * Math.cos(radLat) * pi);
			double mgLat = lat + dLat;
			double mgLon = lon + dLon;
			localHashMap.put("lon", Double.valueOf(mgLon));
			localHashMap.put("lat", Double.valueOf(mgLat));
			return localHashMap;
		}
	}

	public Map gcj2wgs(double lon, double lat)
	{
		Map localHashMap = new HashMap();
		double lontitude = lon - (((Double)transform(lon, lat).get("lon")).doubleValue() - lon);
		double latitude = lat - (((Double)transform(lon, lat).get("lat")).doubleValue() - lat);
		localHashMap.put("lon", Double.valueOf(lontitude));
		localHashMap.put("lat", Double.valueOf(latitude));
		return localHashMap;
	}

}
